package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.system.model.entity.SysDept;
import cn.lingyangwl.agile.system.model.rqrs.dept.DeptListReq;
import cn.lingyangwl.agile.system.model.rqrs.dept.DeptResp;
import cn.lingyangwl.agile.system.model.rqrs.dept.DeptUpdateReq;
import cn.lingyangwl.agile.system.model.rqrs.role.RoleDeptTreeListRS;
import cn.lingyangwl.agile.system.model.rqrs.user.UserDeptListReq;
import cn.lingyangwl.agile.system.service.SysDeptService;
import cn.lingyangwl.framework.core.response.Resp;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 部门信息
 * 
 * @author shenguangyang
 */
@RestController
@RequestMapping("/dept")
public class SysDeptController {
    @Resource
    private SysDeptService deptService;

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/get")
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    public Resp<SysDept> get(@RequestParam(value = "id") Long deptId) {
        return Resp.ok(deptService.getDeptById(deptId));
    }

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    public Resp<List<DeptResp>> list(DeptListReq req) {
        List<DeptResp> resp = deptService.listDept(req);
        return Resp.ok(resp);
    }

    /**
     * 获取部门下拉树列表
     * 如果指定了ids, 则不会构建树
     * 如果没有指定ids, 查询全部并且构建树
     */
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    @GetMapping("/listTree")
    public Resp<List<DeptResp>> listTree(DeptListReq req) {
        return Resp.ok(deptService.listDeptTree(req));
    }

    /**
     * 查询我的部门树 (我的 --> 指的使当前登录的用户)
     */
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    @GetMapping("/listMyDeptTree")
    public Resp<List<DeptResp>> listUserDeptTree(DeptListReq req) {
        UserDeptListReq dto = new UserDeptListReq(SecurityUtils.getUserId());
        dto.setDeptName(req.getDeptName());
        List<DeptResp> deptList = deptService.listUserDept(dto);
        return Resp.ok(deptService.buildDeptTreeSelect(deptList));
    }

    /**
     * 异步查询部门list
     * @param parentId 父id
     * @param ids 前端回显是传递
     */
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    @GetMapping("/listTreeSync")
    public Resp<List<DeptResp>> listTreeSync(@RequestParam(name = "pid", required = false) String parentId,
                                             @RequestParam(name = "ids", required = false) String ids) {
        return Resp.ok(deptService.listDeptTreeSync(parentId, ids));
    }

    /**
     * 查询部门树
     *
     */
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    @GetMapping(value = "/listDeptTreeForRole")
    public Resp<RoleDeptTreeListRS> listDeptTreeForRole() {
        List<DeptResp> deptList = deptService.listDept(new DeptListReq());
        RoleDeptTreeListRS deptTreeSelectDTO = new RoleDeptTreeListRS();
        deptTreeSelectDTO.setIds(deptList.stream().map(DeptResp::getId).collect(Collectors.toList()));
        deptTreeSelectDTO.setTreeList(deptService.buildDeptTreeSelect(deptList));
        return Resp.ok(deptTreeSelectDTO);
    }

    /**
     * 加载对应角色中的部门列表树
     */
    @GetMapping(value = "/listDeptInRole")
    @RamResource(action = "sys:dept:list", name = "查询部门", type = ActionType.READ)
    public Resp<List<String>> listDeptInRole(@RequestParam("roleId") Long roleId) {
        return Resp.ok(deptService.listDeptTreeInRole(roleId).stream().map(String::valueOf).collect(Collectors.toList()));
    }

    /**
     * 新增部门
     */
    @RamResource(action = "sys:dept:save", name = "保存部门", type = ActionType.WRITE)
    @PostMapping("/save")
    public Resp<?> save(@Validated @RequestBody SysDept dept) {
        return deptService.saveDept(dept) > 0 ? Resp.ok() : Resp.fail("新增部门失败");
    }

    /**
     * 修改部门
     */
    @RamResource(action = "sys:dept:update", name = "更新部门", type = ActionType.WRITE)
    @PutMapping("/update")
    public Resp<?> update(@Validated @RequestBody DeptUpdateReq req) {
        return deptService.updateDept(req) ? Resp.ok() : Resp.fail("更新部门失败");
    }

    /**
     * 删除部门
     */
    @RamResource(action = "sys:dept:delete", name = "删除部门", type = ActionType.WRITE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> remove(@RequestParam(value = "ids") String[] deptIds) {
        deptService.deleteBatch(deptIds);
        return Resp.ok();
    }
}